﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Frame.Lib.Core.Base;
using Frame.Lib.Enums;

namespace Frame.Lib.Core.Attach
{
    public class ImageBO:CommonOperate<ImageInfo>
    {
        private ImageDAL _image;
        //private TableName _tableName;

        protected override BaseDAL DAL
        {
            get
            {
                return _image;
            }
        }

        public ImageBO(string tableName):base(tableName)
        {
           // this._tableName = tableName;
            _image = new ImageDAL(tableName);
        }

        public ImageBO(STableName tableName)
            : this(tableName.ToString())
        {
           
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bindID"></param>
        /// <param name="flag">可为空，用于一个表中有多种类型的图片</param>
        /// <returns></returns>
        public List<ImageInfo> GetList(int bindID, string flag)
        {
            List<ImageInfo> list = _image.GetList(bindID, flag);
            return list;
        }

        public override void Delete(IEnumerable<int> ids)
        {
            foreach (int id in ids)
            {
                this.Delete(id);
            }
        }

        public void DeleteByBindID(object bindID)
        {
            IEnumerable<int> ids = _image.GetIDs(bindID);
            this.Delete(ids);
        }

        public override void Delete(int id)
        {
            ImageInfo info = this.GetInfo(id);

            this.DeleteImage(info.OriginImageAbsPath);
            this.DeleteImage(info.SmallImageAbsPath);
            this.DeleteImage(info.BigImageAbsPath);

            base.Delete(id);
        }

        private void DeleteImage(string path)
        {
            string phyPath = System.Web.HttpContext.Current.Server.MapPath(path);
            if (File.Exists(phyPath))
            {
                File.Delete(phyPath);
            }
        }

        public int GetMaxOrder(int bindID)
        {
            return _image.GetMaxOrder(bindID);
        }

        
    }
}
